<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:mo="https://gitee.com/aun/tentime">
<head th:replace="/common/template :: header(~{::title},~{::link},~{::style})">
    <link rel="stylesheet" th:href="@{/lib/zTree_v3/css/zTreeStyle/zTreeStyle.css}" type="text/css">
</head>
<body>
<div class="layui-form tentime-compile">
    <form id="inputForm" th:object="${entity}" class="layui-form"> <!-- th:action="@{/system/dept/save}" -->
        <input id="id" type="hidden" name="id" th:if="${entity}" th:value="${entity.id}"/>
        <div class="layui-form-item">
        	<div class="layui-inline">
	            <label class="layui-form-label required">父级部门</label>
	            <div class="layui-input-inline">
	                <input class="layui-input select-tree" lay-verify="required" th:attr="data-url=@{/system/dept/list}, data-value=${pDept?.id}" type="text" name="pid"  placeholder="请输入父级部门" th:value="${pDept?.title}">
	            </div>
            </div>
	        <div class="layui-inline">
	            <label class="layui-form-label required">机构类型</label>
	            <div class="layui-input-inline">
			      <select class="tentime-search-select" lay-verify="required" name="type" mo:dict="DEPT_TYPE" mo-selected="${entity.type}" mo-empty="全部"></select>
	            </div>
	        </div>
        </div>
        <div class="layui-form-item">
        	<div class="layui-inline">
	            <label class="layui-form-label required">部门编码</label>
	            <div class="layui-input-inline">
	                <input class="layui-input" lay-verify="required" type="text" id="code" name="code"  placeholder="请输入编码" th:value="${entity?.code}">
	            </div>
            </div>
        	<div class="layui-inline">
	            <label class="layui-form-label required">部门名称</label>
	            <div class="layui-input-inline">
	                <input class="layui-input" lay-verify="required" type="text" name="title"  placeholder="请输入标题" th:value="${entity?.title}">
	            </div>
            </div>
        </div>   
        <div class="layui-form-item">
            <label class="layui-form-label">排序(之后)</label>
            <div class="layui-input-inline">
                <select class="select-sort" name="sort"
                        th:attr="data-url=@{/system/dept/sortList}, data-id=${entity?.id}, data-sort=${entity?.sort}" lay-verify="sort"></select>
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-inline">
                <textarea placeholder="请输入内容" class="layui-textarea" name="remark" style="width:450px;">[[${entity?.remark}]]</textarea>
            </div>
        </div>
        <div class="layui-form-item tentime-finally">
            <button class="layui-btn layui-btn-sm layui-btn-primary" lay-submit lay-filter="submit_button" shiro:hasAnyPermissions="system:dept:add,system:dept:edit"><i class="fa fa-check-circle"></i> 保存</button>
            <button class="layui-btn layui-btn-sm layui-btn-primary" onclick="closeWin();" type="button"><i class="fa fa-times-circle"></i> 关闭</button>
        </div>
    </form>
</div>
<script th:replace="/common/template :: common"></script>
<script type="text/javascript" th:src="@{/lib/zTree_v3/js/jquery.ztree.core.min.js}"></script>
<script type="text/javascript" th:src="@{/js/tentimeTree.js}"></script>
<script type="text/javascript">
	var pid = '';
    layui.use(['form'], function () {
        window.form = layui.form;
        // 初始化排序下拉选项
        pid = $(".select-tree").data('value');
        if (pid != undefined){
            sortRender({id: pid});
        }
        form.on("submit(submit_button)", function (data) {
        	$("#inputForm").ajaxSubmit({
				type : "post"
	            ,url : ctx + '/system/dept/saveEntity'
	            ,success : function(data) {
	            	var code = data.code;
	            	if (code == 200) {
	            		parent.layer.msg('数据保存成功!', {icon: 1});
	            		closeWin();
	            		window.parent.expandById(pid);
	            	} else {
	            		parent.layer.msg('数据验证失败!', {icon: 2});
	            	}
	            }
			});
       		return false;
       	});
    });
    
    // 初始化下拉树
    $.fn.selectTree({
        rootTree: '顶级',
        // 选中后事件
        onSelected: sortRender
    });

    // 更新渲染排序下拉选项以及动态改变下级编码
    function sortRender(treeNode) {
        pid = treeNode.id;
        var sort = $(".select-sort");
        var id = sort.data('id') ? sort.data('id') : 0;
        var url = sort.data('url') + "/" + pid + "/" + id;
        $.get(url, function (result) {
            var options = '';
            var sortNum = Object.keys(result).length;
            if(pid === $(".select-tree").data('value') && sort.data('sort')){
                sortNum = sort.data('sort') - 1;
            }
            result[0] = "首位";
            for(var key in result){
                var selected = sortNum == key ? "selected=''" : "";
                options += "<option value='"+ key +"' " + selected + ">"+ result[key] +"</option>";
            }
            sort.html(options);
            form.render('select');
        });
        // 设置默认的code
        var id = $('#id').val();
        if (id === '') {//新增的状态再设置
	        var url_code = ctx + '/system/dept/getCode/' + pid;
	        $.get(url_code, function (result) {
	        	$('#code').val(result);
	        });
        }
    }
</script>
</body>
</html>